package com.softeem.controller;

import com.softeem.entity.WorkOrder;
import com.softeem.mapper.WorkOrderMapper;
import com.softeem.model.LayuiPagingResult;
import com.softeem.model.ParamModel;
import com.softeem.model.WorkOrderModel;
import com.softeem.service.WorkOrderService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.*;

@Controller
@RequestMapping("/order")
public class WorkOrderController {

    @Autowired
    private WorkOrderService orderService;

    @RequestMapping("/newOrder")
    @ResponseBody
    public Map<String, Object> newOrder(WorkOrder workOrder,MultipartFile file) throws IOException {
        if (!file.isEmpty()) {
            String fileName = UUID.randomUUID().toString() + file.getOriginalFilename();
            // 上传文件
            file.transferTo(new File("D:\\SecondProject\\resource\\", fileName));
            workOrder.setPath(fileName);
        }
        workOrder.setCreateTime(new Date());
        workOrder.setNowTime(new Date());
        Map<String, Object> result = new HashMap();
        result.put("state",orderService.addWorkOrder(workOrder));
        return result;
    }

    @RequestMapping("/getOrders")
    @ResponseBody
    public LayuiPagingResult<WorkOrderModel> getOrdersDynamical(ParamModel paramModel){
        System.out.println(paramModel);
       return orderService.getOrdersDynamical(paramModel);
    }

    @RequestMapping("/getOrder")
    @ResponseBody
    public WorkOrderModel getOrderById(Integer orderId){
        return orderService.getWorkOrderById(orderId);
    }


    @RequestMapping("/detail")
    public String detail(@Param("orderId") Integer orderId, @Param("req") HttpServletRequest req, @Param("resp") HttpServletResponse resp){
        WorkOrderModel workOrderModel = orderService.getWorkOrderById(orderId);
        req.getSession().setAttribute("orderInfo",workOrderModel);
        System.out.println(workOrderModel);
        return "order/detail";
    }
}
